<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="tarinfo-objects.html" />
<link rel="prev" href="module-tarfile.html" />
<link rel="parent" href="module-tarfile.html" />
<link rel="next" href="tarinfo-objects.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>12.5.1 TarFile Objects </title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="12.5 tarfile  "
  href="module-tarfile.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="12.5 tarfile  "
  href="module-tarfile.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="12.5.2 tarinfo Objects"
  href="tarinfo-objects.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-tarfile.html">12.5 tarfile  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-tarfile.html">12.5 tarfile  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="tarinfo-objects.html">12.5.2 TarInfo Objects</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h2><a name="SECTION0014510000000000000000"></a><a name="tarfile-objects"></a>
<br>
12.5.1 TarFile Objects 
</h2>

<p>
The <tt class="class">TarFile</tt> object provides an interface to a tar archive. A tar
archive is a sequence of blocks. An archive member (a stored file) is made up
of a header block followed by data blocks. It is possible, to store a file in a
tar archive several times. Each archive member is represented by a
<tt class="class">TarInfo</tt> object, see <em class="citetitle"
 >TarInfo Objects</em> (section
<a href="tarinfo-objects.html#tarinfo-objects">12.5.2</a>) for details.

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-2405' xml:id='l2h-2405' class="class">TarFile</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>name
                           </var><big>[</big><var>, mode</var><big>[</big><var>, fileobj</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
    Open an <em>(uncompressed)</em> tar archive <var>name</var>.
    <var>mode</var> is either <code>'r'</code> to read from an existing archive,
    <code>'a'</code> to append data to an existing file or <code>'w'</code> to create a new
    file overwriting an existing one. <var>mode</var> defaults to <code>'r'</code>.

<p>
If <var>fileobj</var> is given, it is used for reading or writing data.
    If it can be determined, <var>mode</var> is overridden by <var>fileobj</var>'s mode.
    <var>fileobj</var> will be used from position 0.
    <div class="note"><b class="label">Note:</b>
<var>fileobj</var> is not closed, when <tt class="class">TarFile</tt> is closed.
    </div>
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2406' xml:id='l2h-2406' class="method">open</tt></b>(</nobr></td>
  <td><var>...</var>)</td></tr></table></dt>
<dd>
    Alternative constructor. The <tt class="function">open()</tt> function on module level is
    actually a shortcut to this classmethod. See section&nbsp;<a href="module-tarfile.html#module-tarfile">12.5</a>
    for details.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2407' xml:id='l2h-2407' class="method">getmember</tt></b>(</nobr></td>
  <td><var>name</var>)</td></tr></table></dt>
<dd>
    Return a <tt class="class">TarInfo</tt> object for member <var>name</var>. If <var>name</var> can
    not be found in the archive, <tt class="exception">KeyError</tt> is raised.
    <div class="note"><b class="label">Note:</b>
If a member occurs more than once in the archive, its last
        occurrence is assumed to be the most up-to-date version.
    </div>
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2408' xml:id='l2h-2408' class="method">getmembers</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
    Return the members of the archive as a list of <tt class="class">TarInfo</tt> objects.
    The list has the same order as the members in the archive.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2409' xml:id='l2h-2409' class="method">getnames</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
    Return the members as a list of their names. It has the same order as
    the list returned by <tt class="method">getmembers()</tt>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2410' xml:id='l2h-2410' class="method">list</tt></b>(</nobr></td>
  <td><var>verbose=True</var>)</td></tr></table></dt>
<dd>
    Print a table of contents to <code>sys.stdout</code>. If <var>verbose</var> is
    <tt class="constant">False</tt>, only the names of the members are printed. If it is
    <tt class="constant">True</tt>, output similar to that of <b class="program">ls -l</b> is produced.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2411' xml:id='l2h-2411' class="method">next</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
    Return the next member of the archive as a <tt class="class">TarInfo</tt> object, when
    <tt class="class">TarFile</tt> is opened for reading. Return <code>None</code> if there is no
    more available.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2412' xml:id='l2h-2412' class="method">extractall</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>path</var><big>[</big><var>, members</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
    Extract all members from the archive to the current working directory
    or directory <var>path</var>. If optional <var>members</var> is given, it must be
    a subset of the list returned by <tt class="method">getmembers()</tt>.
    Directory informations like owner, modification time and permissions are
    set after all members have been extracted. This is done to work around two
    problems: A directory's modification time is reset each time a file is
    created in it. And, if a directory's permissions do not allow writing,
    extracting files to it will fail.
    
<span class="versionnote">New in version 2.5.</span>

</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2413' xml:id='l2h-2413' class="method">extract</tt></b>(</nobr></td>
  <td><var>member</var><big>[</big><var>, path</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
    Extract a member from the archive to the current working directory,
    using its full name. Its file information is extracted as accurately as
    possible.
    <var>member</var> may be a filename or a <tt class="class">TarInfo</tt> object.
    You can specify a different directory using <var>path</var>.
    <div class="note"><b class="label">Note:</b>
Because the <tt class="method">extract()</tt> method allows random access to a tar
    archive there are some issues you must take care of yourself. See the
    description for <tt class="method">extractall()</tt> above.
    </div>
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2414' xml:id='l2h-2414' class="method">extractfile</tt></b>(</nobr></td>
  <td><var>member</var>)</td></tr></table></dt>
<dd>
    Extract a member from the archive as a file object.
    <var>member</var> may be a filename or a <tt class="class">TarInfo</tt> object.
    If <var>member</var> is a regular file, a file-like object is returned.
    If <var>member</var> is a link, a file-like object is constructed from the
    link's target.
    If <var>member</var> is none of the above, <code>None</code> is returned.
    <div class="note"><b class="label">Note:</b>
The file-like object is read-only and provides the following methods:
        <tt class="method">read()</tt>, <tt class="method">readline()</tt>, <tt class="method">readlines()</tt>,
        <tt class="method">seek()</tt>, <tt class="method">tell()</tt>.
    </div>
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2415' xml:id='l2h-2415' class="method">add</tt></b>(</nobr></td>
  <td><var>name</var><big>[</big><var>, arcname</var><big>[</big><var>, recursive</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
    Add the file <var>name</var> to the archive. <var>name</var> may be any type
    of file (directory, fifo, symbolic link, etc.).
    If given, <var>arcname</var> specifies an alternative name for the file in the
    archive. Directories are added recursively by default.
    This can be avoided by setting <var>recursive</var> to <tt class="constant">False</tt>;
    the default is <tt class="constant">True</tt>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2416' xml:id='l2h-2416' class="method">addfile</tt></b>(</nobr></td>
  <td><var>tarinfo</var><big>[</big><var>, fileobj</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
    Add the <tt class="class">TarInfo</tt> object <var>tarinfo</var> to the archive.
    If <var>fileobj</var> is given, <code><var>tarinfo</var>.size</code> bytes are read
    from it and added to the archive.  You can create <tt class="class">TarInfo</tt> objects
    using <tt class="method">gettarinfo()</tt>.
    <div class="note"><b class="label">Note:</b>
On Windows platforms, <var>fileobj</var> should always be opened with mode
    <code>'rb'</code> to avoid irritation about the file size.
    </div>
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2417' xml:id='l2h-2417' class="method">gettarinfo</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>name</var><big>[</big><var>,
                               arcname</var><big>[</big><var>, fileobj</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
    Create a <tt class="class">TarInfo</tt> object for either the file <var>name</var> or
    the file object <var>fileobj</var> (using <tt class="function">os.fstat()</tt> on its
    file descriptor).  You can modify some of the <tt class="class">TarInfo</tt>'s
    attributes before you add it using <tt class="method">addfile()</tt>.  If given,
    <var>arcname</var> specifies an alternative name for the file in the
    archive.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2418' xml:id='l2h-2418' class="method">close</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
    Close the <tt class="class">TarFile</tt>. In write mode, two finishing zero
    blocks are appended to the archive.
</dl>

<p>
<dl><dt><b><tt id='l2h-2419' xml:id='l2h-2419' class="member">posix</tt></b></dt>
<dd>
    If true, create a POSIX 1003.1-1990 compliant archive. GNU
    extensions are not used, because they are not part of the POSIX
    standard.  This limits the length of filenames to at most 256,
    link names to 100 characters and the maximum file size to 8
    gigabytes. A <tt class="exception">ValueError</tt> is raised if a file exceeds
    this limit.  If false, create a GNU tar compatible archive.  It
    will not be POSIX compliant, but can store files without any
    of the above restrictions. 
    
<span class="versionnote">Changed in version 2.4:
<var>posix</var> defaults to <tt class="constant">False</tt>.</span>

</dl>

<p>
<dl><dt><b><tt id='l2h-2420' xml:id='l2h-2420' class="member">dereference</tt></b></dt>
<dd>
    If false, add symbolic and hard links to archive. If true, add the
    content of the target files to the archive.  This has no effect on
    systems that do not support symbolic links.
</dl>

<p>
<dl><dt><b><tt id='l2h-2421' xml:id='l2h-2421' class="member">ignore_zeros</tt></b></dt>
<dd>
    If false, treat an empty block as the end of the archive. If true,
    skip empty (and invalid) blocks and try to get as many members as
    possible. This is only useful for concatenated or damaged
    archives.
</dl>

<p>
<dl><dt><b><tt id='l2h-2422' xml:id='l2h-2422' class="member">debug=0</tt></b></dt>
<dd>
    To be set from <code>0</code> (no debug messages; the default) up to
    <code>3</code> (all debug messages). The messages are written to
    <code>sys.stderr</code>.
</dl>

<p>
<dl><dt><b><tt id='l2h-2423' xml:id='l2h-2423' class="member">errorlevel</tt></b></dt>
<dd>
    If <code>0</code> (the default), all errors are ignored when using
    <tt class="method">extract()</tt>.  Nevertheless, they appear as error messages
    in the debug output, when debugging is enabled.  If <code>1</code>, all
    <em>fatal</em> errors are raised as <tt class="exception">OSError</tt> or
    <tt class="exception">IOError</tt> exceptions.  If <code>2</code>, all <em>non-fatal</em>
    errors are raised as <tt class="exception">TarError</tt> exceptions as well.
</dl>

<p>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="12.5 tarfile  "
  href="module-tarfile.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="12.5 tarfile  "
  href="module-tarfile.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="12.5.2 tarinfo Objects"
  href="tarinfo-objects.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-tarfile.html">12.5 tarfile  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-tarfile.html">12.5 tarfile  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="tarinfo-objects.html">12.5.2 TarInfo Objects</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
